package com.qk.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {

    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public PageResult<Clue> listClue(ClueDto clueDto) {
        Page<Clue> page = clueMapper.listClues (new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<Clue>(page.getTotal(),page.getRecords());
    }

    @Override
    public Clue getClueById(Integer id) {
        return clueMapper.getClueById(id);
    }

    /**
     * 线索跟进
     */
    @Override
    @Transactional(rollbackFor = Exception.class)//开启事务
    public void trackClue(Clue clue) {
        //1、更新线索信息
        clue.setStatus(3);//跟进中
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);

        //2、添加线索跟踪记录
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());//线索id
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());//跟进人ID - 当前登录用户ID
        clueTrackRecord.setSubject(clue.getSubject());
        clueTrackRecord.setLevel(clue.getLevel());
        clueTrackRecord.setRecord(clue.getRecord());
        clueTrackRecord.setNextTime(clue.getNextTime());
        clueTrackRecord.setType(1);
        clueTrackRecord.setCreateTime(LocalDateTime.now());

        //insert 添加需要一个对象，上边new ClueTrackRecord(),然后将信息封装到对象中传入
        clueTrackRecordMapper.insert(clueTrackRecord);
    }



    /**
     * 伪线索
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void pseudoClues(Integer id, MarkFalseClueDto markFalseClueDto) {
       //1、更新线索信息
        Clue clue = new Clue();
        clue.setId(id);
        clue.setStatus(4);
        clue.setUpdateTime(LocalDateTime.now());
       clueMapper.updateById(clue);

       //2、添加线索跟踪记录
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(id);
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());//跟进人ID - 当前登录用户ID
        clueTrackRecord.setType(0);
        clueTrackRecord.setFalseReason(markFalseClueDto.getReason());
        clueTrackRecord.setRecord(markFalseClueDto.getRemark());
        clueTrackRecord.setCreateTime(LocalDateTime.now());

//        int i = 1/0;

        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    /**
     *转商机
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void toBusiness(Integer id) {
        //1、更新线索信息
        Clue clue = clueMapper.selectById(id);
        clue.setStatus(5);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);

        //2、添加商机数据(根据线索表数据添加)
        Business business = new Business();
        BeanUtils.copyProperties(clue,business);
        business.setId(null);
        business.setUserId(null);
        business.setStatus(1);
        business.setClueId(id);
        business.setNextTime(null);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.insert(business);
    }

    @Override
    public PageResult<Clue> listCluePool(ClueDto clueDto) {
        Page<Clue> page = clueMapper.listCluePool(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<Clue>(page.getTotal(),page.getRecords());
    }


}
